﻿using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net;
using Infrastructure.CurrentData;

namespace Infrastructure.Managers
{
    /// <summary>
    /// Manages document covers
    /// </summary>
    public class CoverManager
    {
        /// <summary>
        /// Returns the cover folder for the current bookshelf
        /// </summary>
        public string CoverFolder
        {
            get
            {
                Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(Current.BookshelfPath));

                return Path.Combine(Path.GetDirectoryName(Current.BookshelfPath), "Covers");
            }
        }

        /// <summary>
        /// Generates a filenames in GUID.ext format
        /// </summary>
        /// <param name="path">Path to the existing file/URI</param>
        /// <returns>GUID.ext</returns>
        public string GenerateFileName(string path)
        {
            Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(path));

            return Guid.NewGuid() + Path.GetExtension(path);
        }

        /// <summary>
        /// Creates cover directory if not exists.
        /// </summary>
        public void CheckCoverDirectory()
        {
            Contract.Ensures(Directory.Exists(CoverFolder));

            if (!Directory.Exists(CoverFolder))
            {
                Directory.CreateDirectory(CoverFolder);
            }
        }

        /// <summary>
        /// Download a cover from the Internet
        /// </summary>
        /// <param name="path">Uri to the cover</param>
        /// <returns>Path to the stored image</returns>
        public string Download(Uri path)
        {
            Contract.Requires<ArgumentNullException>(path != null);
            Contract.Ensures(File.Exists(Contract.Result<string>()));

            CheckCoverDirectory();

            // Generate paths
            string url = path.AbsoluteUri;
            string newFileName = GenerateFileName(url);
            string newFilePath = Path.Combine(CoverFolder, newFileName);
            
            // Download file
            using (WebClient client = new WebClient())
            {
                client.DownloadFile(path, newFilePath);
            }

            return newFilePath;
        }

        /// <summary>
        /// Moves a file to the cover directory
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string MoveFile(Uri path)
        {
            Contract.Requires<ArgumentNullException>(path != null);
            Contract.Ensures(File.Exists(Contract.Result<string>()));
            
            // Generate paths
            string localPath = path.AbsolutePath;
            string newFileName = GenerateFileName(localPath);
            string newFilePath = Path.Combine(CoverFolder, newFileName);

            File.Copy(localPath, newFilePath);

            return newFilePath;
        }


    }
}